package cn.kone.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import cn.kone.user.domain.User;
import cn.kone.user.domain.query.UserQuery;
import cn.kone.user.mapper.UserMapper;
import cn.kone.user.service.IUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    @Transactional
    public void addUser(User user) {
        //设置盐值
        String salt = RandomUtil.randomString(32);
        //将糊涂的盐值赋值给用户的盐值
        user.setSalt(salt);
        //将密码进行加密
        String md5PWD = SecureUtil.md5(user.getPassword() + salt);
        user.setPassword(md5PWD);
        userMapper.addUser(user);
    }

    @Override
    @Transactional
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    @Transactional
    public void deleteUser(Long id) {
        userMapper.deleteUser(id);
    }

    @Override
    public List<User> pageQuery(UserQuery userQuery) {
        PageHelper.startPage(userQuery.getCurrentPage(),userQuery.getPageSize());
        return userMapper.pageQuery(userQuery);
    }

    @Override
    public void batchDeleteUser(Long[] ids) {
        userMapper.batchDeleteUser(ids);
    }
}
